#include "videostrategy.h"

VideoStrategy::VideoStrategy(int w, int h)
    : ICaptureStrategy(w, h), itsVideoList(), itsFrameCount(0)
{

}

int VideoStrategy::prepareVideoCapture()
{
    do
    {
        SelectVideo();
        if(videoPath.isEmpty())
        {
            QMessageBox mesg;
            mesg.warning(this,"Tip","视频文件无效!");
            break;
        }

        itsVideo.open(videoPath.toStdString().c_str());// 使用视频
        if(!itsVideo.isOpened())
        {
            QMessageBox mesg;
            mesg.warning(this,"Tip","视频文件无法打开, : "+videoPath);
            break;
        }

        itsWidth = static_cast<int>(itsVideo.get(cv::CAP_PROP_FRAME_WIDTH));
        itsHeight = static_cast<int>(itsVideo.get(cv::CAP_PROP_FRAME_HEIGHT));

        qDebug() << "video  width:" << itsWidth << "  height:" << itsHeight;

        itsVideo >> itsBaseImage;
        cv::cvtColor(itsBaseImage, itsBaseImage, cv::COLOR_RGB2GRAY);
        itsBaseImage = imgMinusAvg(itsBaseImage);
        cv::imshow("base", itsBaseImage);
        if(itsBaseImage.empty())
        {
            QMessageBox mesg;
            mesg.warning(this,"Tip","抓取基准图失败!");
            break;
        }

        itsVideoList.clear();
        itsVideoList.resize(0);
        itsFrameCount = 0;
        while(1)
        {
            cv::Mat image;
            itsVideo >> image;
            if(image.empty())
            {
                QMessageBox mesg;
                mesg.warning(this,"Tip","读取视频文件成功!");
                qDebug() << "Frame count : " << itsVideoList.size();
                break;
            }
            itsVideoList.emplace_back(std::move(image));
        }

    } while(false);

    return 0;
}

int VideoStrategy::getFrame(cv::Mat& frame)
{
    int ret = 0;

    do
    {
        if(!itsVideo.isOpened())
        {
            qDebug() << "Video not open";
            ret = -1;
            break;
        }

        if(itsFrameCount >= itsVideoList.size())
        {
            qDebug() << "play done";
            ret = -3;
            break;
        }

        frame = itsVideoList[itsFrameCount].clone();
        if(frame.empty())
        {
            qDebug() << "frame empty";
            ret = -2;
            break;
        }
        else
        {
            ret = 0;
            itsFrameCount++;
            break;
        }
    } while(false);

    return ret;
}

void VideoStrategy::SelectVideo()
{
    videoPath = QFileDialog::getOpenFileName(this, tr("Choose Video"), ".", tr("Video Files(*.avi *.mp4)"));
}

int VideoStrategy::getFrameIndex()
{
    return itsFrameCount;
}

void VideoStrategy::setFrameIndex(int index)
{
    int total = itsVideoList.size();
    float percentage = (float)index/100.0f;
    float tmp = (float)total * percentage;
    itsFrameCount = (int)tmp;
}

void VideoStrategy::frameForward()
{
    itsFrameCount++;
    if(itsFrameCount >= itsVideoList.size())
    {
        itsFrameCount = itsVideoList.size() - 1;
    }
}

void VideoStrategy::frameBackoff()
{
    itsFrameCount--;
    if(itsFrameCount < 0)
    {
        itsFrameCount = 0;
    }
}

int VideoStrategy::getFrameTotal()
{
    return itsVideoList.size();
}
